//
//  BaseModel.m
//  MovieProject
//
//  Created by 张琪( ⊙ o ⊙ )！ on 13-9-7.
//  Copyright (c) 2013年 张琪( ⊙ o ⊙ )！. All rights reserved.
//

#import "BaseModel.h"

@implementation BaseModel

- (id)initContentWith:(NSDictionary *)dic
{
    self = [super init];
    if (self) {
        
        [self dicToObject:dic];
    }
    return self;
}
//title -setTitle:
- (SEL)stringTosel:(NSString *)modelKey
{
    NSString *first = [[modelKey substringToIndex:1] uppercaseString];
    NSString *end = [modelKey substringFromIndex:1];
    NSString *methedStr = [NSString stringWithFormat:@"set%@%@:",first,end];
    return NSSelectorFromString(methedStr);
}

//创建dic和对象之间的映射关系
- (NSDictionary *)dicObjectAtt:(NSDictionary *)jsonDic
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:jsonDic.count];
    for (id key in jsonDic) {
        [dic setObject:key forKey:key];//object:属性的名字 ，key:字典中的KEY
    }
    return dic;
}

//把字典里面的值通过映射找到对象的属性并赋值
- (void)dicToObject:(NSDictionary *)dic
{
    for (id key in dic) {
        id modelKey = [[self dicObjectAtt:dic]objectForKey:key];
        if (modelKey) {
            //根据字符串拿到set方法
            SEL action = [self stringTosel:modelKey];
            if ([self respondsToSelector:action]) {
                id value = [dic objectForKey:key];
                [self performSelector:action withObject:value];
            }
        }
    }
}


@end
